package com.sws.attendance.service.impl;

import java.util.Date;
import java.util.List;

import com.sws.common.utils.DateUtils;
import com.sws.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sws.attendance.mapper.StuAttendanceMapper;
import com.sws.attendance.domain.StuAttendance;
import com.sws.attendance.service.IStuAttendanceService;

/**
 * 学生签到记录Service业务层处理
 * 
 * @author cx
 * @date 2025-06-16
 */
@Service
public class StuAttendanceServiceImpl implements IStuAttendanceService 
{
    @Autowired
    private StuAttendanceMapper stuAttendanceMapper;

    /**
     * 查询学生签到记录
     * 
     * @param id 学生签到记录主键
     * @return 学生签到记录
     */
    @Override
    public StuAttendance selectStuAttendanceById(Long id)
    {
        return stuAttendanceMapper.selectStuAttendanceById(id);
    }

    /**
     * 查询学生签到记录列表
     * 
     * @param stuAttendance 学生签到记录
     * @return 学生签到记录
     */
    @Override
    public List<StuAttendance> selectStuAttendanceList(StuAttendance stuAttendance)
    {
        return stuAttendanceMapper.selectStuAttendanceList(stuAttendance);
    }

    /**
     * 新增学生签到记录
     * 
     * @param stuAttendance 学生签到记录
     * @return 结果
     */
    @Override
    public int insertStuAttendance(StuAttendance stuAttendance)
    {
        stuAttendance.setCreateTime(DateUtils.getNowDate());

        stuAttendance.setDelFlag(0);
        return stuAttendanceMapper.insertStuAttendance(stuAttendance);
    }

    /**
     * 修改学生签到记录
     * 
     * @param stuAttendance 学生签到记录
     * @return 结果
     */
    @Override
    public int updateStuAttendance(StuAttendance stuAttendance)
    {
        stuAttendance.setUpdateTime(DateUtils.getNowDate());
        return stuAttendanceMapper.updateStuAttendance(stuAttendance);
    }

    /**
     * 批量删除学生签到记录
     * 
     * @param ids 需要删除的学生签到记录主键
     * @return 结果
     */
    @Override
    public int deleteStuAttendanceByIds(Long[] ids) {
        int count = 0;
        for (Long id : ids) {
            StuAttendance stuAttendance = stuAttendanceMapper.selectStuAttendanceById(id);
            if (stuAttendance != null) {

                // 删除本地记录
                stuAttendance.setDelFlag(1);
                stuAttendance.setUpdateBy(SecurityUtils.getUsername());
                stuAttendance.setUpdateTime(new Date());
                count += stuAttendanceMapper.updateStuAttendance(stuAttendance);
            }
        }
        return count;
    }

    /**
     * 删除学生签到记录信息
     * 
     * @param id 学生签到记录主键
     * @return 结果
     */
    @Override
    public int deleteStuAttendanceById(Long id) {
        // 改为更新删除标志，而不是物理删除
        StuAttendance stuAttendance = stuAttendanceMapper.selectStuAttendanceById(id);
        if (stuAttendance == null) {
            return 0;
        }

        stuAttendance.setUpdateTime(DateUtils.getNowDate());
        stuAttendance.setUpdateBy(SecurityUtils.getUsername());
        return stuAttendanceMapper.updateDelFlagById(id);
    }


}
